package com.ruoyi.pingjiao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.pingjiao.domain.Student;
import com.ruoyi.pingjiao.domain.Teacher;
import com.ruoyi.pingjiao.mapper.CourseclassMetaclassMapper;
import com.ruoyi.pingjiao.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pingjiao.mapper.CourseclassMapper;
import com.ruoyi.pingjiao.domain.Courseclass;
import com.ruoyi.pingjiao.service.ICourseclassService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 上课班级管理Service业务层处理
 *
 * @author 胡浩鹏
 * @date 2022-10-20
 */
@Service
public class CourseclassServiceImpl implements ICourseclassService
{
    @Autowired
    private CourseclassMapper courseclassMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseclassMetaclassMapper courseclassMetaclassMapper;
    /**
     * 查询上课班级管理
     *
     * @param courseclassId 上课班级管理主键
     * @return 上课班级管理
     */
    @Override
    public Courseclass selectCourseclassByCourseclassId(Long courseclassId)
    {
        return courseclassMapper.selectCourseclassByCourseclassId(courseclassId);
    }

    /**
     * 查询上课班级管理列表
     *
     * @param courseclass 上课班级管理
     * @return 上课班级管理
     */
    @Override
    public List<Courseclass> selectCourseclassList(Courseclass courseclass)
    {
        List<Courseclass> courseclasses = courseclassMapper.selectCourseclassList(courseclass);

        /*
        System.out.println(courseclasses);

        List<Courseclass> list = new ArrayList<>();

        Courseclass courseclass1 ;

        System.out.println("===========");
        for (Courseclass cc : courseclasses){
            System.out.println("教师列表");
            System.out.println(cc.getTeacherList());
            for (Teacher t : cc.getTeacherList()){
                //courseclass1 = cc;
                cc.setTeacher(t);
            }
            list.add(cc);
        }

        System.out.println("-课程列表--");
        System.out.println(list);
         */

        return courseclasses;
    }

    @Override
    public List<Courseclass> selectCourseclassList1(Courseclass courseclass) {
        return courseclassMapper.selectCourseclassList1(courseclass);
    }

    /**
     * 新增上课班级管理
     *
     * @param courseclass 上课班级管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCourseclass(Courseclass courseclass)
    {
        System.out.println(courseclass);
        courseclass.setCreateTime(DateUtils.getNowDate());
        int i = courseclassMapper.insertCourseclass(courseclass);
        //根据上课班级号查询上课班级id
        Long courseclassId= courseclassMapper.selectCourseclassIdByCourseclassNo(courseclass.getCourseclassNo());
        courseclass.setCourseclassId(courseclassId);
        for (Teacher teacher:courseclass.getTeacherList()
             ) {
            Map<String,Long>map=new HashMap<>();
            map.put("teacherId",teacher.getTeacherId());
            map.put("courseclassId",courseclassId);
            courseclassMapper.deleteByCourseclassId(courseclassId);
            courseclassMapper.saveTeacherList1(map);
        }
//        courseclassMapper.saveTeacherList(courseclass);

        List<Student> students = studentMapper.selectStudentListByMetaClassIds(courseclass.getMetaclassList());
        courseclass.setStudentList(students);

        for (Student student:courseclass.getStudentList()
        ) {
            Map<String,Long>map=new HashMap<>();
            map.put("studentId",student.getStudentId());
            map.put("courseclassId",courseclassId);
            courseclassMapper.deleteByCourseclassId(courseclassId);
            courseclassMapper.saveStudentList1(map);
        }
//        courseclassMapper.saveStudentList(courseclass);
        courseclassMetaclassMapper.deleteCourseclassMetaclassByCourseclassIdNo(courseclass.getCourseclassId());
        Map<String,Object>map=new HashMap<>();
        map.put("courseclassId",courseclass.getCourseclassId());
        map.put("metaclassId",courseclass.getMeataclassId());
        courseclassMetaclassMapper.addCourseclassMetaclass(map);
        return i;
    }

    /**
     * 修改上课班级管理
     *
     * @param courseclass 上课班级管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCourseclass(Courseclass courseclass)
    {
        courseclass.setUpdateTime(DateUtils.getNowDate());
        int i = courseclassMapper.updateCourseclass(courseclass);

        if (courseclass.getMetaclassList() != null && courseclass.getMetaclassList().size() != 0){
            List<Student> students = studentMapper.selectStudentListByMetaClassIds(courseclass.getMetaclassList());
            courseclass.setStudentList(students);
        }else {
            courseclass.setStudentList(new ArrayList<Student>());
        }

        courseclassMapper.saveTeacherList(courseclass);
        courseclassMapper.saveStudentList(courseclass);
        return i;
    }

    /**
     * 批量删除上课班级管理
     *
     * @param courseclassIds 需要删除的上课班级管理主键
     * @return 结果
     */
    @Override
    public int deleteCourseclassByCourseclassIds(Long[] courseclassIds)
    {
        return courseclassMapper.deleteCourseclassByCourseclassIds(courseclassIds);
    }

    /**
     * 删除上课班级管理信息
     *
     * @param courseclassId 上课班级管理主键
     * @return 结果
     */
    @Override
    public int deleteCourseclassByCourseclassId(Long courseclassId)
    {
        return courseclassMapper.deleteCourseclassByCourseclassId(courseclassId);
    }
    /**
     * @author hpf
     * 更新成绩
     */
    public  int updateCourseclassScore(String score, Long courseclassId)
    {
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("courseclassScore",score);
        map.put("courseclassId",courseclassId);
        return  courseclassMapper.updateCourseclassScore(map);
    }

    @Override
    public Courseclass selectCourseclassByCourseclassIdplus(Long courseclassId) {
        return courseclassMapper.selectCourseclassByCourseclassIdplus(courseclassId);
    }

    @Override
    public Object getPjInfoByUserId(Long userId) {
        Long studentId = studentMapper.selectStudentIdByUserId(userId);
        List<Courseclass>courseclasses=courseclassMapper.getPjInfoByStudentId(studentId);
        return courseclasses;
    }
}
